package org.cybzacg.encryption.model;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 注册结果类
 * 封装加密器注册操作的结果信息
 * 
 * @author encryption team
 * @version 1.0
 */
public class RegistrationResult {
    
    private final boolean success;
    private final String className;
    private final String message;
    private final LocalDateTime timestamp;
    private final Exception exception;
    
    private RegistrationResult(Builder builder) {
        this.success = builder.success;
        this.className = builder.className;
        this.message = builder.message;
        this.timestamp = builder.timestamp;
        this.exception = builder.exception;
    }
    
    /**
     * 获取注册是否成功
     * 
     * @return 是否成功
     */
    public boolean isSuccess() {
        return success;
    }
    
    /**
     * 获取类名
     * 
     * @return 类名
     */
    public String getClassName() {
        return className;
    }
    
    /**
     * 获取消息
     * 
     * @return 消息
     */
    public String getMessage() {
        return message;
    }
    
    /**
     * 获取时间戳
     * 
     * @return 时间戳
     */
    public LocalDateTime getTimestamp() {
        return timestamp;
    }
    
    /**
     * 获取异常信息
     * 
     * @return 异常信息，可能为null
     */
    public Exception getException() {
        return exception;
    }
    
    /**
     * 创建成功的注册结果
     * 
     * @param className 类名
     * @param message 成功消息
     * @return 注册结果
     */
    public static RegistrationResult success(String className, String message) {
        return new Builder()
                .success(true)
                .className(className)
                .message(message)
                .build();
    }
    
    /**
     * 创建失败的注册结果
     * 
     * @param className 类名
     * @param message 失败消息
     * @return 注册结果
     */
    public static RegistrationResult failure(String className, String message) {
        return new Builder()
                .success(false)
                .className(className)
                .message(message)
                .build();
    }
    
    /**
     * 创建失败的注册结果（带异常）
     * 
     * @param className 类名
     * @param message 失败消息
     * @param exception 异常
     * @return 注册结果
     */
    public static RegistrationResult failure(String className, String message, Exception exception) {
        return new Builder()
                .success(false)
                .className(className)
                .message(message)
                .exception(exception)
                .build();
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RegistrationResult that = (RegistrationResult) o;
        return success == that.success &&
                Objects.equals(className, that.className) &&
                Objects.equals(message, that.message) &&
                Objects.equals(timestamp, that.timestamp);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(success, className, message, timestamp);
    }
    
    @Override
    public String toString() {
        return "RegistrationResult{" +
                "success=" + success +
                ", className='" + className + '\'' +
                ", message='" + message + '\'' +
                ", timestamp=" + timestamp +
                '}';
    }
    
    /**
     * 建造者类
     */
    public static class Builder {
        private boolean success = false;
        private String className;
        private String message;
        private LocalDateTime timestamp = LocalDateTime.now();
        private Exception exception;
        
        public Builder success(boolean success) {
            this.success = success;
            return this;
        }
        
        public Builder className(String className) {
            this.className = className;
            return this;
        }
        
        public Builder message(String message) {
            this.message = message;
            return this;
        }
        
        public Builder timestamp(LocalDateTime timestamp) {
            this.timestamp = timestamp;
            return this;
        }
        
        public Builder exception(Exception exception) {
            this.exception = exception;
            return this;
        }
        
        public RegistrationResult build() {
            return new RegistrationResult(this);
        }
    }
}
